/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月27日         Root.Lu
 */

package com.coocaa.launcher.statusplugins.net;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPlugin;
import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;
import com.coocaa.x.framework.app.BootUpRunnable;
import com.coocaa.x.framework.utils.LogUtils;

public class StatusPluginNet extends StatusBarPlugin {
    public enum NET_STATE {
        DISCONNECT, WIFI, ETHERNET,
    }

    public interface IStatusPluginNet {
        void updateState(NET_STATE state);

        void updateWifiRSSI(int rssi);
    }

    public static StatusPluginNet instance = new StatusPluginNet();
    private StatusPluginNetView view = null;
    private IStatusPluginNet net = null;

    private StatusPluginNet() {
        super("statusplugin.net");
        // TODO Auto-generated constructor stub
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                LogUtils.d("statusplugin", "sunny net action :" + action);
                postAction(runnable, 1000);
            } else if (action.equals(BootUpRunnable.QUICK_RESUME_ACTION)) {
                LogUtils.d("statusplugin", "sunny net action : QUICK_RESUME_ACTION");
                net.updateState(NET_STATE.DISCONNECT);
                postAction(runnable, 1000 * 60 * 2);
            }
        }
    };

    private Runnable runnable = new Runnable() {

        private void updateRSSI() {
            int rssi = -100;
            WifiManager wifi_service = (WifiManager) mContext
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifi_service.getConnectionInfo();
            if (wifiInfo != null)
                rssi = wifiInfo.getRssi();
            net.updateWifiRSSI(rssi);
            postAction(runnable, 1000 * 60);
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (info != null && info.isAvailable() && info.isConnected()) {
                int type = info.getType(); // wifi==1
                if (type == 1) {
                    LogUtils.d("statusplugin", "sunny net info:" + info);
                    net.updateState(NET_STATE.WIFI);
                    updateRSSI();
                } else {
                    net.updateState(NET_STATE.ETHERNET);
                }
            } else {
                LogUtils.d("statusplugin", "sunny net no info:" + info);
                postAction(runnable, 1000 * 60);
                net.updateState(NET_STATE.DISCONNECT);
            }
        }
    };

    @Override
    protected void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        try{
            IntentFilter mFilter = new IntentFilter();
            mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            mFilter.addAction(BootUpRunnable.QUICK_RESUME_ACTION);
            LogUtils.d("statusplugin", "sunny net boot onCreate");
            mContext.registerReceiver(mReceiver, mFilter);
        }catch (Exception e){
            e.printStackTrace();
        }
        postAction(runnable, 1000);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        try{
            mContext.unregisterReceiver(mReceiver);
        }catch (Exception e){
            e.printStackTrace();
        }
        super.onDestroy();
        view = null;
        net = null;
        LogUtils.d("", "sunny net boot onDestroy");
    }

    @Override
    public StatusBarPluginView inflate(String pattern) {
        // TODO Auto-generated method stub
        if (view == null) {
            view = new StatusPluginNetView(mContext, getName());
            net = (IStatusPluginNet) view;
        }
        postAction(runnable, 1000);
        return view;
    }

}
